﻿using System;
using UnityEngine;

namespace LightGameLibs
{
    public class MBSingleton : MonoBehaviour
    {
        public enum MBCallbackType
        {
            NONE = -1,

            AWAKE = 0,
            ENABLE = 1,
            START = 2,
            DISABLE = 3,
            UPDATE = 4,
            DESTROY = 5,
            RENDER = 6,

            AMOUNT
        }

        private static MBSingleton s_singleton;

        private Action[] m_MBCallback = new Action[(int)MBCallbackType.AMOUNT];

        void Awake()
        {
            InvokeCallback(MBCallbackType.AWAKE);
        }

        void OnEnable()
        {
            InvokeCallback(MBCallbackType.ENABLE);
        }

        void Start()
        {
            InvokeCallback(MBCallbackType.START);
        }

        void OnDisable()
        {
            InvokeCallback(MBCallbackType.DISABLE);
        }

        void Update()
        {
            InvokeCallback(MBCallbackType.UPDATE);
        }

        void OnDestroy()
        {
            InvokeCallback(MBCallbackType.DESTROY);
        }

        void OnRenderObject()
        {
            InvokeCallback(MBCallbackType.RENDER);
        }

        private void InvokeCallback(MBCallbackType type)
        {
            m_MBCallback[(int)type].Invoke();
        }

        public void AddListener(MBCallbackType type, Action callback)
        {
            m_MBCallback[(int)type] += callback;
        }

        public void RemoveListener(MBCallbackType type, Action callback)
        {
            m_MBCallback[(int)type] -= callback;
        }

        public static MBSingleton GetMBSingleton()
        {
            if (null == s_singleton)
            {
                DebugMgr.Log(DebugTag.UTILITY, "Mono not exist, Finding a mono");

                s_singleton = FindObjectOfType<MBSingleton>();
            }

            return s_singleton;
        }
    }
}

